package com.travel.server.manager.impl.command.order.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.order.data.ExportChargeTargetListRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.order.data.QueryChargeTargetListResponse.ChargeTargetItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\12\9 0009 10:23
 * @Desc:
 */
public class ExportChargeTargetListCommand extends AbstractCommand<ExportChargeTargetListRequest, EmptyResponse> {

    @Autowired
    private QueryChargeTargetListCommand queryChargeTargetListCommand;

    @Override
    protected void checkInput(ExportChargeTargetListRequest request) {
        checkTimeInput(request);
    }

    @Override
    public EmptyResponse handle(ExportChargeTargetListRequest request) {
        checkInput(request);
        List<ChargeTargetItem> list = queryChargeTargetListCommand.makeTargetList(request);
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }

        List<String> title = Arrays.asList("订单负责人", "累计成单数", "目标", "差额", "完成率");
        List<List<Object>> rows = new ArrayList<>();
        for(ChargeTargetItem item : list){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getOrderCharge());
            objects.add(item.getOrder());
            objects.add(item.getTarget());
            objects.add(item.getDifference());
            objects.add(item.getComplete());
            rows.add(objects);
        }
        ExcelData excelData = new ExcelData();
        excelData.setTitles(title);
        excelData.setRows(rows);
        excelData.setName("订单负责人销售目标统计");

        ExcelUtil.exportExcel(getHttpServletResponse(), "订单负责人销售目标统计", excelData);

        return new EmptyResponse();
    }
}
